package com.csaba.connector;

import java.util.HashSet;
import java.util.ServiceLoader;
import java.util.Set;

import com.csaba.connector.BankServiceFactory.FactoryProvider;

/**
 * Factory provider that is based on the java.util.ServiceLoader mechanism. The plugins (to work with this provider)
 * has to create service definition in the META-INF/services/com.csaba.connector.ServicePluginConfiguration.
 * 
 * Sample: <code><pre>
 * BankServiceFactory.serProvider( new ServiceLoaderProvider() );
 * </pre></code>
 * 
 * @see java.util.ServiceLoader
 * @author gyenesge
 *
 */
public class ServiceLoaderProvider implements FactoryProvider
{
	ServiceLoader<ServicePluginConfiguration> loader;

	public ServiceLoaderProvider()
	{
		loader = ServiceLoader.load(ServicePluginConfiguration.class);
	}

	@Override
	public Set<ServicePluginConfiguration> getPlugins()
	{
		final HashSet<ServicePluginConfiguration> plugins = new HashSet<ServicePluginConfiguration>();
		for ( final ServicePluginConfiguration plugin : loader )
		{
			plugins.add(plugin);
		}
		return plugins;
	}

}
